/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.procedure;

import java.util.*;
import java.awt.*;
import javax.swing.*;

import jdbcmanager.swing.table.*;
import jdbcmanager.dbtools.datatypes.*;

/**
 * Table with Stored Procedures properties that is shown inside the
 * MDI window at its bottom area.
 * 
 * @author Francisco Morero Peyrona
 */

final class TablePanel extends JPanel
{
   private JScrollPane   spTable = new JScrollPane();
   private JxTableVector table   = new JxTableVector();

   //-------------------------------------------------------------------------//
   
   public TablePanel( StoredProcedureDefinition sp )
   {
      initTable( sp );
      initGUI();
   }

   //-------------------------------------------------------------------------//

   private void initTable( StoredProcedureDefinition sp )
   {
      Vector vHead = new Vector();
             vHead.add( "Name" );
             vHead.add( "Return" );
             vHead.add( "SqlDataType" );
             vHead.add( "ReturnType" );
             vHead.add( "Precision" );
             vHead.add( "ByteLen" );
             vHead.add( "Scale" );
             vHead.add( "Radix" );
             vHead.add( "Nullable" );
             vHead.add( "Remarks" );

      StoredProcedureDefinition.Column[] spc = sp.getColumns();
      Vector vData = new Vector();
      
      for( int n = 0; n < spc.length; n++ )
      {
         Vector vRow = new Vector();
                vRow.add( spc[n].getName() );
                vRow.add( spc[n].getReturn() );
                vRow.add( spc[n].getSqlDataType() );
                vRow.add( spc[n].getReturnTypeName() );
                vRow.add( new Integer( spc[n].getPrecision() ) );
                vRow.add( new Integer( spc[n].getByteLength() ) );
                vRow.add( new Short( spc[n].getScale() ) );
                vRow.add( new Short( spc[n].getRadix() ) );
                vRow.add( spc[n].getNullable() );
                vRow.add( spc[n].getRemarks() );

         vData.add( vRow );
      }

      this.table.setHead( vHead );
      this.table.setData( vData );
      this.table.adjustColumnsWidth();
   }

   private void initGUI()
   {
      setBorder( BorderFactory.createEmptyBorder( 5,5,5,5 ) );
      setLayout( new BorderLayout() );

      this.spTable.setBorder( BorderFactory.createLineBorder( Color.black, 1 ) );
      this.spTable.getViewport().add( this.table );

      add( new JLabel( " Columns Information" ), BorderLayout.NORTH );
      add( this.spTable, BorderLayout.CENTER );
   }
}
